/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.solestac.view;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.TableModel;
import org.colswe.solestac.control.AdministrationControl;
import org.colswe.solestac.entity.ActualUser;
import org.colswe.solestac.VO.UserVO;

/**
 *
 * @author David
 */
public class ButtonDelete extends DefaultCellEditor {

    protected JButton button;
    private String label;
    private boolean isPushed;
    private UserVO user = null;
    private long ides[];
    private JTable table;
    private Object[][] data;
    private ActualUser actualUser = ActualUser.getInstance();

    public ButtonDelete(JCheckBox checkBox, JTable table, long[] ids, Object[][]data) {
        super(checkBox);
        this.table = table;
        this.ides = ids;
        this.data = data;
        button = new JButton();
        button.setOpaque(true);
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fireEditingStopped();
            }
        });
    }

    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {
        if (isSelected) {
            button.setForeground(table.getSelectionForeground());
            button.setBackground(table.getSelectionBackground());
        } else {
            button.setForeground(table.getForeground());
            button.setBackground(table.getBackground());
        }
        label = (value == null) ? "" : value.toString();
        button.setText(label);
        isPushed = true;
        return button;
    }

    public Object getCellEditorValue() {
        if (isPushed) {

            int i = table.getSelectedRow();
            long id = ides[i];
            String name = (String) data[i][0];
            String document= (String) data[i][1];
            String role= (String) data[i][2];

            if (actualUser.sameUser(name, id)) {
                JOptionPane.showMessageDialog(null,"No puede eliminar el usuario escogido","",JOptionPane.WARNING_MESSAGE);
            } else if (JOptionPane.showConfirmDialog(null, "¿Está seguro que desea eliminar el usuario?", "¿Seguro?", 1, 2) == 0) {


                user = new UserVO();
                user.setId(id);
                user.setUsername(name);
                user.setDocument(document);
                user.setRole(role);

                AdministrationControl delete = new AdministrationControl();
                boolean result = delete.deleteUser(user);
                actualUser.getPrincipal().updateFrame(new CRUDUser());
            }


        }
        isPushed = false;
        return new String(label);
    }

    public boolean stopCellEditing() {
        isPushed = false;
        return super.stopCellEditing();
    }

    protected void fireEditingStopped() {
        super.fireEditingStopped();
    }
}
